
package com.yuhanginfo.modules.sys.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yuhanginfo.common.service.CrudService;
import com.yuhanginfo.modules.sys.entity.PartsChange;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.utils.UserUtils;
import com.yuhanginfo.modules.sys.dao.PartsChangeDao;

/**
 * 配件互换Service
 * @author yinhao
 * @version 2017-11-29
 */
@Service
@Transactional(readOnly = true)
public class PartsChangeService extends CrudService<PartsChangeDao, PartsChange> {
	public static final Logger logger =  LoggerFactory.getLogger(PartsChangeService.class);
	@Autowired
	private PartsChangeDao partsChangeDao;
	

	
	
	
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void save(String groupId,String[] partsCode) throws Exception {
		boolean stu=false;
		String groupIds="";
		User user = UserUtils.getUser();
		Date now = new Date();
		List<PartsChange> list = new ArrayList<>();
		try{
		if(null != groupId && !"".equals(groupId)){
			partsChangeDao.deleteByGroupId(groupId);
		}else{
			groupIds = String.valueOf(now.getTime());
			stu=true;
		}
		
		for(int i=0;i<partsCode.length;i++){
			PartsChange partsChange = new PartsChange();
			if(stu){
				partsChange.setGroupId(groupIds);
			}else{
				
				partsChange.setGroupId(groupId);
			}
	
			partsChange.setPartsCode(partsCode[i]);
			partsChange.setCreateBy(user);
			partsChange.setCreateDate(now);
			partsChange.setUpdateBy(user);
			partsChange.setUpdateDate(now);
			partsChange.setDelFlag("0");
			list.add(partsChange);
		}
		if(!list.isEmpty()){
			partsChangeDao.batchInsert(list);
		}
		}catch(Exception ex){
			logger.debug("",ex);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(PartsChange partsChange) {
		super.delete(partsChange);
	}
	
	public List<PartsChange> findByGroupId(PartsChange partsChange) {
		return partsChangeDao.findByGroupId(partsChange);
	}
	
	@Transactional(readOnly = false)
	public void del(String groupId) {
		//删除商品门店关系
		partsChangeDao.deleteByGroupId(groupId);
}
}